package com.du.dupload;

import java.io.File;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.channels.UnsupportedAddressTypeException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.turn.ttorrent.client.Client;
import com.turn.ttorrent.client.Client.ClientState;
import com.turn.ttorrent.client.SharedTorrent;

public class TorrentDownloader2 implements IResourceDownloader {

	private static final double MAX_SPEED = 10000.;
	private static final double MAX_UPLOAD = 222.1;
	private static final String OUT = AppConfig.getInstance().getOutDir();
	private String torrentPath;
	private Client c;
	private static final Logger logger = LoggerFactory
			.getLogger(TorrentDownloader2.class);

	TorrentDownloader2(String torrentPath) {
		super();
		this.torrentPath = torrentPath;
	}

	@Override
	public float getProgressPercent() {
		return c.getTorrent().getCompletion();
	}

	@Override
	public void close() {

	}

	@Override
	public void startDownload() {
		initializeLoading();
		c.download();
		// System.exit(1);

	}

	@Override
	public boolean isCompleted() {
		return c.getState() == ClientState.DONE;
	}

	@Override
	public boolean isError() {
		return c.getState() == ClientState.ERROR;
	}

	@Override
	public void stopDownload() {
		c.stop();
	}

	@Override
	public void initializeLoading() {
		try {
			c = new Client(getIPv4Address(null), SharedTorrent.fromFile(
					new File(torrentPath), new File(OUT)));
		} catch (UnsupportedAddressTypeException | IOException e) {
			e.printStackTrace();
		}

		c.setMaxDownloadRate(MAX_SPEED);
		c.setMaxUploadRate(MAX_UPLOAD);
		Runtime.getRuntime().addShutdownHook(
				new Thread(new Client.ClientShutdown(c, null)));
	}

	@Override
	public List<String> getPath() {
		List<String> list = c.getTorrent().getFilenames();
		List<String> copy = new ArrayList<String>();
		for (String s : list) {
			copy.add(AppConfig.getInstance().getOutDir() + s);
		}
		return copy;
	}

	private static Inet4Address getIPv4Address(String iface)
			throws SocketException, UnsupportedAddressTypeException,
			UnknownHostException {
		if (iface != null) {
			Enumeration<InetAddress> addresses = NetworkInterface.getByName(
					iface).getInetAddresses();
			while (addresses.hasMoreElements()) {
				InetAddress addr = addresses.nextElement();
				if (addr instanceof Inet4Address) {
					return (Inet4Address) addr;
				}
			}
		}

		InetAddress localhost = InetAddress.getLocalHost();
		if (localhost instanceof Inet4Address) {
			return (Inet4Address) localhost;
		}

		throw new UnsupportedAddressTypeException();
	}

	@Override
	public String getName() {
		return c.getTorrent().getName();
	}

}
